﻿#include "complexsetform.h"
#include "ui_complexsetform.h"
#include "myhelper.h"
#include "iconhelper.h"
#include "singleton.h"
#include "myapp.h"


constexpr int WS_PORT = 8108;
ComplexSetForm::ComplexSetForm(QString devType, QWidget* parent ) :
    QWidget(parent),
    ui(new Ui::ComplexSetForm)
{
    m_devType = devType;
    ui->setupUi(this);
    this->initForm();

    this->on_device_manage_clicked();

}

ComplexSetForm::~ComplexSetForm()
{
    delete ui;
}
void ComplexSetForm::closeEvent(QCloseEvent* event)
{
    Q_UNUSED(event);

    qDebug() << "ComplexSetForm close";
}


void ComplexSetForm::initForm()
{
    //按钮切换图标
    QList<QToolButton*> btns;
    QList<int> pixChar;
    pixChar << 0xf233 << 0xf0c2 << 0xf080 << 0xf030 << 0xf093 ;

    btns << ui->device_manage << ui->algo_manage << ui->coalspeed_manage << ui->snap_manage << ui->upload_manage ;
    QString normalBgColor = "rgb(12, 12, 12)";
    QString darkBgColor = "rgb(12, 12, 12)";
    QString normalTextColor = "#54626F";
    QString darkTextColor = "#086FF7";
    IconHelper::Instance()->setStyle(ui->widget, btns, pixChar, 24, 30, 30, "right", 3, "#029FEA", normalBgColor, darkBgColor, normalTextColor, darkTextColor);
    ui->snap_manage->setVisible(false);
    ui->coalspeed_manage->setVisible(false);
    this->setsomehidden();
}
void ComplexSetForm::setsomehidden()
{
    if(m_devType == "AI高速相机")
    {
        ui->snap_manage->setVisible(true);
        ui->coalspeed_manage->setVisible(true);
    }
}

void ComplexSetForm::on_device_manage_clicked()
{
    //系统设置
    if(ui->stackedWidget->findChildren<QWidget*>("系统设置").count())
    {
        ui->stackedWidget->setCurrentWidget(commonsetform);
        return;
    }
    commonsetform = new CommonSetForm(this);
    commonsetform->setObjectName("系统设置");

    connect(Singleton::Instance(), &Singleton::sigGetDeviceInfo, this, &ComplexSetForm::onGetdevicetype);
    connect(Singleton::Instance(), &Singleton::sigGetDeviceInfo, commonsetform, &CommonSetForm::onGetDeviceInfo);
    connect(Singleton::Instance(), &Singleton::sigGetDeviceRunInfo, commonsetform, &CommonSetForm::onGetDeviceRunInfo);
    connect(Singleton::Instance(), &Singleton::sigGetNetworkParam, commonsetform, &CommonSetForm::onGetNetworkParam);
    connect(Singleton::Instance(), &Singleton::sigGetAlgoInfo, commonsetform, &CommonSetForm::onGetAlgoInfo);

    connect(Singleton::Instance(), &Singleton::sigSetNetworkParamSuccess, this, [ = ]()
    {
        //设置网络参数成功
        //请求是否重启
        QMessageBox m_box(QMessageBox::Question, QString("请求"), QString("修改成功,是否重启设备"), QMessageBox::Ok | QMessageBox::Cancel);
        if(m_box.exec() == QMessageBox::Ok)
        {
            commonsetform->on_btn_rebootDev_clicked();

        }
    });
    connect(Singleton::Instance(), &Singleton::sigSyncDateTimeSuccess, this, [ = ]()
    {
        //同步时间成功
        commonsetform->on_btn_GetDeviceRunInfo_clicked();
    });

    ui->stackedWidget->addWidget(commonsetform);
    ui->stackedWidget->setCurrentWidget(commonsetform);
    QJsonObject body;
    Singleton::Instance()->onSendParam("GetDeviceInfo", body);
}

void ComplexSetForm::on_algo_manage_clicked()
{
    if(ui->stackedWidget->findChildren<QWidget*>("算法设置").count())
    {
        ui->stackedWidget->setCurrentWidget(algoform);
        return;
    }
    algoform = new AlgoForm(m_devType, this);
    algoform->setObjectName("算法设置");
    connect(Singleton::Instance(), &Singleton::sigGetDeviceInfo, algoform, &AlgoForm::onChnInfo);
    ui->stackedWidget->addWidget(algoform);
    ui->stackedWidget->setCurrentWidget(algoform);
    QJsonObject body;
    Singleton::Instance()->onSendParam("GetDeviceInfo", body);
}

void ComplexSetForm::on_coalspeed_manage_clicked()
{
    if(ui->stackedWidget->findChildren<QWidget*>("煤量调速").count())
    {
        ui->stackedWidget->setCurrentWidget(coalform);
        return;
    }
    coalform = new CoalForm(this);
    coalform->setObjectName("煤量调速");
    ui->stackedWidget->addWidget(coalform);
    ui->stackedWidget->setCurrentWidget(coalform);
    connect(Singleton::Instance(), &Singleton::sigAlarmImage, coalform, &CoalForm::onAlarmImage);
    connect(Singleton::Instance(), &Singleton::sigCoalSpdValue, coalform, &CoalForm::onCoalSpdValue);
    connect(Singleton::Instance(), &Singleton::sigGetCoalSpdParam, coalform, &CoalForm::onCoalSpdParam);

    connect(Singleton::Instance(), &Singleton::sigGetColidarPileCoalParam, coalform, &CoalForm::onGetColidarPileCoalParam);
}

void ComplexSetForm::on_snap_manage_clicked()
{
    if(ui->stackedWidget->findChildren<QWidget*>("相机设置").count())
    {
        ui->stackedWidget->setCurrentWidget(cameraform);
        return;
    }
    cameraform = new CameraForm(this);
    cameraform->setObjectName("相机设置");
    ui->stackedWidget->addWidget(cameraform);
    ui->stackedWidget->setCurrentWidget(cameraform);
    connect(Singleton::Instance(), &Singleton::sigGetCameraParam, cameraform, &CameraForm::onGetCameraParam);
    connect(Singleton::Instance(), &Singleton::sigAlarmImage, cameraform, &CameraForm::onAlarmImage);

}
void ComplexSetForm::on_upload_manage_clicked()
{
    if(ui->stackedWidget->findChildren<QWidget*>("上传升级").count())
    {
        ui->stackedWidget->setCurrentWidget(upform);
        return;
    }
    upform = new DevUpForm(this);
    upform->setObjectName("上传升级");
    ui->stackedWidget->addWidget(upform);
    ui->stackedWidget->setCurrentWidget(upform);
    connect(Singleton::Instance(), &Singleton::sigAppUpgrade, upform, &DevUpForm::onAppUpgrade);
}

void ComplexSetForm::onGetdevicetype(QJsonObject obj)
{
    QString devType = obj.value("devType").toString();
    if(m_devType == "")
    {
        if(devType == "AiCam_H")
        {
            m_devType = myApp::AiCam_H;
        }
        else if(devType == "AiCam_L")
        {
            m_devType = myApp::AiCam_L;
        }
        else if(devType == "Ai_ARM")
        {
            m_devType = myApp::Ai_ARM;
        }
        else if(devType == "Ai_GPU")
        {
            m_devType = myApp::Ai_GPU;
        }
        else if(devType == "Ai_DZFT")
        {
            m_devType = myApp::Ai_DZFT;
        }
    }
    this->setsomehidden();
}
